<template>
	<view class="content">
		<text v-if="state === 0" class="success-icon yticon icon-xuanzhong2"></text>
		<text v-if="state === 2" class="minss"  :class="{active : !disableCodeBtn}" :disabled="disableCodeBtn" >{{codeBtn.text}}</text>
		<text v-if="state === 3" class="success-icon yticon icon-shanchu4"></text>
		<text class="tit">{{msg}}</text>
		
		
		<view class="btn-group">
			
			<navigator url="/pages/order/order?state=0" open-type="redirect" class="mix-btn" v-if="ORDERNUM.substring(0,1)!='Z'">查看订单</navigator>
			<navigator url="/pages/index/index" open-type="switchTab" class="mix-btn hollow">返回首页</navigator>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				ORDERNUM: '',
				msg: '',
				state: 2,
				seconds: 5,
				codeBtn: {
					text: '请稍候...',
					waitingCode: false,
					count: this.seconds
				},
				group:0
			}
		},
		onLoad(options){
			console.log("接收的信息",options)
			this.ORDERNUM = options.ORDERNUM;
			// #ifdef H5
			if(options.out_trade_no){
				this.ORDERNUM = options.out_trade_no
			}
			// #endif
		console.log("单号",this.ORDERNUM)
			// this.loadData1()
			if(options.ISBACK == undefined){
				// #ifdef H5
				if(options.TYPE==6){
					this.laddate()
				}else{
					this.ladcheck();
				}
				// #endif
				// #ifndef H5
				this.laddate()
				// #endif
			}else if(options.ISBACK == 1){
				this.laddate();
			}
			if(this.ORDERNUM.substr(0,1)=='T'){
				this.group = 1
			}
			;
			uni.startPullDownRefresh();
		},
		onPullDownRefresh() {
			setTimeout(function () {
				console.log('refresh');
				uni.stopPullDownRefresh();
			}, 1000);
			
		},
		onShow() {
			
		},
		computed: {
			name() {
				return this.state 
			},
			disableCodeBtn: function (){
				return this.codeBtn.waitingCode ;
			} 
		},
		methods: {
			ladcheck(){
				uni.showModal({
					content: '已经支付？',
					success: (res)=>{
						if(res.confirm){
							this.laddate()
						}else if(res.cancel) {
							uni.switchTab({
								url: '/pages/user/user'
							})
						}
					},
				})
			},
			onBackPress(options) {
				console.log('from:' + options.from)
				if (options.from == 'backButton') {
					return true; 
				}
			},
			laddate(){
				
				uni.showLoading({
					title: '请稍候...',
					mask:true
				})
				this.codeBtn.waitingCode = true;
				this.codeBtn.count = this.seconds;
				this.codeBtn.text = '请勿离开本页面，支付中，请稍候'+this.codeBtn.count + 's';
				let countdown = setInterval( () => {
					if( this.codeBtn.count < 2 ){
						clearInterval(countdown);
						this.loadData1()
						this.loadData(this.ORDERNUM);
						this.codeBtn.waitingCode = false;
					}
					this.codeBtn.count--;
					this.codeBtn.text = '请勿离开本页面，支付中，请稍候'+this.codeBtn.count + 's';
				},1000);
				
			},
			
			async loadData1(){
				this.$http.post("/user/orderstate",{
					SIGNATURE : uni.getStorageSync('SIGNATURE'),
					TOKEN: uni.getStorageSync('userInfo').TOKEN,
					PAYTOKEN: uni.getStorageSync('userInfo').TOKENP,
				}).then((e)=>{
					
					if (e.statusCode === 200 && e.data.STATE === '0') {  
						if(e.data.OBJECT === 1){
						}
				    }  
				})
				.catch((err)=>{
					console.log('request fail', err);
				})
				
			},
			
			//支付结果查询
			async loadData(ORDERNUM){
				var  uri = ''
				if(this.group == 1){
					uri = "/group/query"
				}else{
					uri = "/order/query"
				}
				let SIGNATURE = uni.getStorageSync("SIGNATURE");
				this.$http.post(uri,{
					SIGNATURE : SIGNATURE,
					ORDERNUM : ORDERNUM,
					TOKEN: uni.getStorageSync("userInfo").TOKEN,
					TOKENP:uni.getStorageSync("userInfo").TOKENP,
				}).then((e)=>{
					if(e.data.STATE === '0' && e.statusCode === 200){
						this.msg = '支付成功'
						this.state = 0
						uni.hideLoading();
					}else{
						this.state = 3
						this.msg = '支付失败'
						uni.showToast({title:'支付失败', icon:"none"});
						uni.hideLoading();
					}
				}).catch((err)=>{
					console.log('request fail', '支付失败');
				})
			},
			
		}
	}
</script>

<style lang='scss'>
	.content{
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}
	.success-icon{
		font-size: 160upx;
		color: #fa436a;
		margin-top: 100upx;
	}
	.tit{
		font-size: 38upx;
		color: #303133;
	}
	.minss{
		text-align: center;
		padding-top: 200upx;
	}
	.btn-group{
		padding-top: 100upx;
	}
	.mix-btn {
		margin-top: 30upx;
		display: flex;
		align-items: center;
		justify-content: center;
		width: 600upx;
		height: 80upx;
		font-size: $font-lg;
		color: #fff;
		background-color: $base-color;
		border-radius: 10upx;
		&.hollow{
			background: #fff;
			color: #303133;
			border: 1px solid #ccc;
		}
	}
</style>
